﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using SqlSugar;

namespace SqlServerToClass
{
    public partial class Form1 : Form
    {
        private string defualtPath = "D:\\Models";

        //将引用的dll加入到exe中的方法
        System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
            dllName = dllName.Replace(".", "_");
            if (dllName.EndsWith("_resources")) return null;
            System.Resources.ResourceManager rm = new System.Resources.ResourceManager(GetType().Namespace + ".Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
            byte[] bytes = (byte[])rm.GetObject(dllName);
            return System.Reflection.Assembly.Load(bytes);
        }

        public Form1()
        {
            //将引用的dll加入到exe中
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 浏览按钮点击事件
        /// </summary>
        private void btn_Path_Click(object sender, EventArgs e)
        {
            DialogResult dr = Path.ShowDialog(); //是调用文件浏览器控件；           
            if (dr == System.Windows.Forms.DialogResult.OK) //是判断文件浏览器控件是否返回ok，即用户是否确定选择。如果确定选择，则弹出用户在文件浏览器中选择的路径：                
           
                tbox_Path.Text = Path.SelectedPath;
        }


        /// <summary>
        /// 开始生成点击事件
        /// </summary>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            #region 这里写生成的方法
            //重置按钮不可用
            btn_Reset.Enabled = false;

            //开始生成按钮不可用
            btn_Start.Enabled = false;

            //验证是否为空
            if (string.IsNullOrEmpty(tbox_Server.Text.Trim()) ) { MessageBox.Show("请输入服务器地址"); return; }
            if (string.IsNullOrEmpty(tbox_Catalog.Text.Trim())) { MessageBox.Show("请输入数据库名称"); return; }
            if (string.IsNullOrEmpty(tbox_UserName.Text.Trim())) { MessageBox.Show("请输入用户名"); return; }
            if (string.IsNullOrEmpty(tbox_PassWord.Text.Trim())) { MessageBox.Show("请输入密码"); return; }

            //收集页面的自定义值
            var server = tbox_Server.Text;
            var catalog = tbox_Catalog.Text;
            var userName = tbox_UserName.Text;
            var password = tbox_PassWord.Text;
            var connectionStr = string.Empty;
            connectionStr=string.Format("server={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}",server,catalog,userName,password);
            try
            {
                //调用SqlSugar.dll的方法生成类文件
                using (var db = new SqlSugarClient(connectionStr))
                {

                    db.ClassGenerating.CreateClassFiles(db, defualtPath, "Models");

                };
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                btn_Reset.Enabled = true;
                btn_Start.Enabled = true;
                return;
            }


            #endregion


            lab_Result.Visible = true;
            btn_OpenPath.Visible = true;
            btn_Reset.Enabled = true;
            btn_Start.Enabled = true;

        }

        /// <summary>
        /// 路径文本框改变事件
        /// </summary>
        private void tbox_Path_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbox_Path.Text.Trim()))
            {
                defualtPath = tbox_Path.Text;
            }
        }

        private void btn_OpenPath_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", defualtPath);
        }

        /// <summary>
        /// 重置按钮点击事件
        /// </summary>
        private void btn_Reset_Click(object sender, EventArgs e)
        {
            tbox_Server.Text = string.Empty;
            tbox_Catalog.Text = string.Empty;
            tbox_UserName.Text = "sa";
            tbox_PassWord.Text = string.Empty;
            tbox_Path.Text = "D:\\Models";
            lab_Result.Visible = false;
            btn_OpenPath.Visible = false;
        }
    }
}
